/**
 * 棋子ui
 * 提供棋子的基本属性和棋子的一些状态
 */
let PiecesUi = Hilo.Class.create({
	Extends: Hilo.Bitmap,
	piece: {},
	uiCoo: [],
	sourceQueue: {},
	// 根据不同的棋子大小调整偏移量
	offset: 35,
	pieceSize: 65,
	isSelected: false,
	constructor: function(properties) {
		this.piece = properties.piece;
		this.uiCoo = properties.uiCoo;
		this.sourceQueue = properties.sourceQueue;
		this.pieceSize = properties.pieceSize == undefined ? this.pieceSize : properties.pieceSize;
		this.offset = this.pieceSize / 1.857;
		let code = properties.piece.code;
		let id = properties.id;
		if (id == undefined) {
			id = code;
		}

		// 所有棋子
		let pieceImgId = code.substr(0, code.lastIndexOf("_"));
		PiecesUi.superclass.constructor.call(this, {
			id: id,
			depth: 99,
			height: this.pieceSize,
			width: this.pieceSize,
			image: this.sourceQueue.getContent(pieceImgId),
			pivotY: this.offset,
			pivotX: this.offset,
			x: this.uiCoo[0],
			y: this.uiCoo[1]
		});
	},
	/**
	 * 选中当前棋子的动作
	 */
	selected: function() {
		// 选中棋子后放大一点，然后透明度降低
		this.scaleX = 1.1;
		this.scaleY = 1.1;
		this.alpha = 0.7;

		this.isSelected = true;
	},
	/**
	 * 取消选中当前棋子
	 */
	cancelSelected: function() {
		// 回到初始状态
		if (this.isSelected) {
			this.scaleX = 1;
			this.scaleY = 1;
			this.alpha = 1;
			this.isSelected = false;
		}
	},
	/**
	 * 将棋子从一个位置移动到另一个位置
	 * @param {Object} x 要移动的位置
	 * @param {Object} y 
	 * @param {Object} onComplete 移动完成后的事件
	 */
	move: function(x, y, onComplete) {
		let ticker = new Hilo.Ticker(60);
		ticker.addTick(Hilo.Tween);
		ticker.start();

		// 移动动画在这里写
		let tween = Hilo.Tween.to(this, {
			x: x,
			y: y
		}, {
			duration: 100,
			delay: 0,
			ease: Hilo.Ease.Linear.EaseIn,
			onComplete: function() {
				if (onComplete != undefined) {
					onComplete();
				}
			}
		});
	},
	// /**
	//  * @param {Object} x 要移动的位置
	//  * @param {Object} y 
	//  * @param {Object} onComplete 移动完成后的事件
	//  */
	// move: function(x, y, onComplete) {
	// 	let ticker = new Hilo.Ticker(60);
	// 	ticker.addTick(Hilo.Tween);
	// 	ticker.start();

	// 	// 移动动画在这里写
	// 	let tween = Hilo.Tween.to(this, {
	// 		x: x,
	// 		y: y
	// 	}, {
	// 		duration: 100,
	// 		delay: 0,
	// 		ease: Hilo.Ease.Linear.EaseIn,
	// 		onComplete: function() {
	// 			if (onComplete != undefined) {
	// 				onComplete();
	// 			}
	// 		}
	// 	});
	// },
	// /**
	//  * 选中这个棋子
	//  * @param {Object} moveAblePoint 可移动的所有点位, move为点击移动点后的事件 
	//  */
	// preMove: function(moveAblePoint, move, error) {
	// 	// 在棋盘上描点可移动的位置
	// 	if (moveAblePoint != undefined && moveAblePoint.length > 0) {
	// 		let container = new Hilo.Container({id: "moveable_point",coos: moveAblePoint,depth: 100});

	// 		// 开始显示棋子可走的路径
	// 		moveAblePoint.forEach((p, index) => {

	// 			let Point = Hilo.Class.create({
	// 				Extends: Hilo.Bitmap,
	// 				uiCoo: p.uiCoo,
	// 				move: move,
	// 				code: this.piece.code,
	// 				coo: p.coo,
	// 				constructor: function(porperties) {
	// 					Point.superclass.constructor.call(this, {
	// 						image: porperties.image,
	// 						// rect: [0, 0, this.pieceSize, this.pieceSize],
	// 						// 不知为何这里需要做一点偏移才能位置正确
	// 						x: p.uiCoo[0] + 13,
	// 						y: p.uiCoo[1] + 12,
	// 						width: this.pieceSize,
	// 						width: this.pieceSize,
	// 						pivotY: porperties.offset,
	// 						pivotX: porperties.offset
	// 					});
	// 				}
	// 			});

	// 			let newPoint = new Point({
	// 				image: this.sourceQueue.getContent("waypoint"),
	// 				offset: this.offset,
	// 				offset: this.offset
	// 			});
	// 			newPoint.on(Hilo.event.POINTER_START,function(e){
	// 					e.eventTarget.move(e.eventTarget.code, e.eventTarget.coo);
	// 				});
	// 			container.addChild(newPoint);
	// 		});
	// 		// 将描点加入到棋子容器中.
	// 		this.parent.addChild(container);
	// 		// 将选中的棋子放大并设置一点透明
	// 		this.scaleX = 1.1;
	// 		this.scaleY = 1.1;
	// 		this.alpha = 0.7;

	// 		this.isSelected = true;
	// 	}else{
	// 		error();
	// 	}
	// },
	// /**
	//  * 取消选中
	//  */
	// cancelPreMove: function(){
	// 	if(this.isSelected){
	// 		this.parent.removeChildById("moveable_point");
	// 		this.scaleX = 1;
	// 		this.scaleY = 1;
	// 		this.alpha = 1;
	// 		this.isSelected = false;
	// 	}
	// }
});


export default PiecesUi;
